/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rational.sheet.table.row;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.event.AncestorEvent;

/**
 *
 * @author Overmind
 */
public class RowHeaderAncestorAdapter
    extends AncestorAdapter
{
    private JTable table;
    private JList list;
    
    public RowHeaderAncestorAdapter(JTable table, JList list)
    {
	this.table = table;
	this.list = list;
    }
    
    public JTable getTable()
    {
	return table;
    }
    
    private JScrollPane getScrollPane(AncestorEvent ae)
    {
	try
	{
	    JViewport viewport = (JViewport) table.getParent();
	    return (JScrollPane) viewport.getParent();
	}
	catch(ClassCastException cce)
	{
	    throw new ClassCastException("Table most be install in JScrollPane");
	}
    }

    @Override
    public void ancestorAdded(AncestorEvent ae)
    {
	super.ancestorAdded(ae);

	JScrollPane scrollPane = getScrollPane(ae);
	scrollPane.setRowHeaderView(list);
	
	JButton cornerButton = new JButton();
	cornerButton.addActionListener(new CornerAdapter(table));
	scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, cornerButton);
    }

    @Override
    public void ancestorRemoved(AncestorEvent ae)
    {
	super.ancestorRemoved(ae);
	
	JScrollPane scrollPane = getScrollPane(ae);
	scrollPane.setRowHeaderView(null);
	
	scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, null);
    }
}
